package com.xue.apitest.testcases;
import com.xue.apitest.constant.Constant;
import com.xue.apitest.constant.API;
import com.xue.apitest.constant.Cases;
import com.xue.apitest.constant.WriteBackData;
import com.xue.apitest.utils.ExcelUtils;
import com.xue.apitest.utils.JsonUtils;
import com.xue.apitest.utils.RestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class BaseCase {

    private Map<String, String> map ;

    @BeforeSuite
    public void init(){
        log.info("=================项目自动化开始====================");
        map = new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("menuId","8");
    }

    /**
     * 添加回写对象到集合中
     * @param rowNum   回写的行号
     * @param cellNum  回写的列号
     * @param content  回写的内容
     */
    public void addWriteBackData (int rowNum, int cellNum, String content){
        WriteBackData writeBackData = new WriteBackData(rowNum, cellNum, content);
        ExcelUtils.wbdList.add(writeBackData);
    }

    /**
     * 调用接口
     * @param api   Excel中对应api
     * @param cases Excel中对应cases
     * @return      返回响应体字符串
     */
    public ResponseEntity<?> call(API api, Cases cases){
        log.info("================开始调用接口==============");
        log.info("================接口编号和接口名称:"+ api.getApiNumber()+"=="+api.getApiName()+"=============");
        log.info("================用例编号和用例描述:"+ cases.getCaseNumber()+"=="+cases.getCaseDes()+"========");
        if ("get".equalsIgnoreCase(api.getApiReqMethod())){
            String url = api.getApiUrl();
            return RestTemplateUtils.get(url, map,String.class);
        }else if ("post".equalsIgnoreCase(api.getApiReqMethod())){
            if ("json".equalsIgnoreCase(api.getApiSubmitType())){

                return RestTemplateUtils.postForJson(api.getApiUrl(),map,cases.getParams(),String.class);
            }else {
                return RestTemplateUtils.postForFormData(api.getApiUrl(),map, cases.getParams(), String.class );
            }

        }
        return null;
    }

    /**
     * 执行用例并将响应数据添加到list中
     * @param api   Excel中对应api
     * @param cases Excel中对应cases
     * @return
     */
    public ResponseEntity<?> execute(API api, Cases cases){
        ResponseEntity<?> response = call(api, cases);
        String actualRes = Objects.requireNonNull(JsonUtils.jsonToObj(Objects.requireNonNull(response.getBody()).toString())).getMsg().equals("成功")? "PASS": "FAIL";
        addWriteBackData(Integer.parseInt(cases.getCaseNumber()), Constant.ACTUAL_WRITER_BACK_CELL_NUM_CN, response.getBody().toString());
        addWriteBackData(Integer.parseInt(cases.getCaseNumber()), Constant.ASSERT_RESULT_CELL_NUM_CN, actualRes);
        return response;
    }

    /**
     * 回写数据,关闭资源
     */
    @AfterSuite
    public void finish(){
        log.info("=============批量回写实际响应结果和执行结果=============");
        ExcelUtils.batchWrite();
        log.info("==============项目自动化结束=========================");
    }
}
